package com.test.mooan.labama.dialog_boom.ln_boom;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.test.mooan.labama.R;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseActivity_boom;
import com.test.mooan.labama.adapter_boom.WltActTypeListAdapter_Fr_boom;
import com.test.mooan.labama.bean_boom.config_info_boom.AppShowInfo_boom;
import com.test.mooan.labama.bean_boom.config_info_boom.CountryInfo_boom;
import com.test.mooan.labama.bean_boom.product.Product;
import com.test.mooan.labama.utils_boom.Stringutil_boom;
import com.test.mooan.labama.utils_boom.Threadutils_boom;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;
import com.test.mooan.labama.utils_boom.http_boom.HttpManager_boom;
import com.test.mooan.labama.bean_boom.user_info_boom.ActList_boom;
import com.test.mooan.labama.view_boom.widget_boom.BoomEditText;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class AddBkActDialog2_Cote_boom extends BottomSheetDialog{

    private BaseActivity_boom baseActivity;
    private BoomEditText account1Et, account2Et;

    private ImageView accountTypeIv;
    private TextView accountTypeTv;
    private ViewGroup accountTypeListLayout;

    private int itemHeight =0;

    private String walletChannelStr;

    private WltActTypeListAdapter_Fr_boom walletAccountTypeListAdapter;

    private final ActList_boom.BankAccount bankAccount;
    
    private final String pageTag;

    private final Product product;

    public AddBkActDialog2_Cote_boom(@NonNull Context context, ActList_boom.BankAccount bankAccount, String pageTag, Product product) {
        super(context,R.style.BottomSheetDialogStyle);
        if (context instanceof BaseActivity_boom) baseActivity = (BaseActivity_boom) context;
        this.bankAccount = bankAccount;
        this.pageTag = pageTag;
        if (product==null) product = Product.gtInstance(null);
        this.product = product;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_add_bk_act_2_cote_boom);
        getWindow().setBackgroundDrawableResource(R.color.trans);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView(){

        account1Et = findViewById(R.id.dialog_add_account_2_et_account_1);
        account2Et = findViewById(R.id.dialog_add_account_2_et_account_2);

        account1Et.stEventWord(pageTag+"_PHONE_INPUT");
        account2Et.stEventWord(pageTag+"_PHONE_CONFIRM_INPUT");

        TextView areacodeTv1 = findViewById(R.id.dialog_add_account_2_tv_areacode_1);
        TextView areacodeTv2 = findViewById(R.id.dialog_add_account_2_tv_areacode_2);
        TextView areacodeTv3 = findViewById(R.id.dialog_add_account_2_tv_areacode_3);

        areacodeTv1.setText(CountryInfo_boom.gInstance().gAreaCode());
        areacodeTv2.setText(CountryInfo_boom.gInstance().gAreaCode());
        areacodeTv3.setText(CountryInfo_boom.gInstance().gAreaCode());

//        account1Et.setOnFocusChangeListener((v, hasFocus) -> {
//            if (hasFocus)
//                UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_PHONE_INPUT"));
//            else
//                UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_PHONE_INPUT"),Stringutil_boom.getSafeString(account1Et.getText().toString()));
//        });
//
//        account2Et.setOnFocusChangeListener((v, hasFocus) -> {
//            if (hasFocus)
//                UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_PHONE_CONFIRM_INPUT"));
//            else
//                UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_PHONE_CONFIRM_INPUT"),Stringutil_boom.getSafeString(account2Et.getText().toString()));
//        });

        findViewById(R.id.dialog_add_bank_account_cote_ib_close).setOnClickListener(v -> dismiss());
        findViewById(R.id.dialog_add_account_2_tv_cancel).setOnClickListener(v -> dismiss());

        TextView showAccountTv = findViewById(R.id.dialog_add_account_2_tv_show_account);
        ImageView showAccountIv = findViewById(R.id.dialog_add_account_2_tv_show_account_type);
        if (bankAccount !=null){
            showAccountTv.setText(bankAccount.gWalletMobile());
            Threadutils_boom.getInstance().runOnChildThread(() -> {
                try {
                    if (TextUtils.isEmpty(bankAccount.gWalletLogo())) return;
                    URL url = new URL(bankAccount.gWalletLogo());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        Threadutils_boom.getInstance().runOnUiThread(() -> {
                            if (showAccountIv!=null) showAccountIv.setImageBitmap(bitmap);
                        });
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }

        //初始化运营商选择组件
        //运营商
        ViewGroup accountTypeLayout = findViewById(R.id.dialog_add_account_2_layout_account_type);
        accountTypeIv = findViewById(R.id.dialog_add_account_2_iv_account_type);
        accountTypeTv = findViewById(R.id.dialog_add_account_2_tv_account_type);
        accountTypeListLayout = findViewById(R.id.dialog_add_account_2_layout_account_type_list);

        //初始化运营商选项
        ListView accountTypeListLv = findViewById(R.id.dialog_add_account_2_lv_account_type_list);
        accountTypeLayout.setOnClickListener(v -> {
            accountTypeListLayout.setVisibility(accountTypeListLayout.getVisibility()==View.VISIBLE ? View.GONE:View.VISIBLE);
            if (accountTypeLayout.getVisibility() == View.VISIBLE)
                UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_TYPE_CLICK"));
        });
        walletAccountTypeListAdapter = new WltActTypeListAdapter_Fr_boom(getContext());
        accountTypeListLv.setAdapter(walletAccountTypeListAdapter);
        accountTypeListLv.setOnItemClickListener((parent, view, position, id) -> {

            accountTypeListLayout.setVisibility(View.GONE);
            AppShowInfo_boom.WalletChannel walletChannel = walletAccountTypeListAdapter.getItem(position);
            walletChannelStr = walletChannel.gValue();

            UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_TYPE_CLICK"),walletChannel.gDesc());

            accountTypeTv.setText(walletChannel.gDesc());

            //获取图标
            if (walletChannel.gBitmap()!=null){
                accountTypeIv.setImageBitmap(walletChannel.gBitmap());
            }else {
                Threadutils_boom.getInstance().runOnChildThread(() -> {
                    try {
                        if (TextUtils.isEmpty(walletChannel.gLogo())) return;
                        URL url = new URL(walletChannel.gLogo());
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setConnectTimeout(5000);
                        connection.setRequestMethod("GET");
                        if (connection.getResponseCode()==200){
                            InputStream inputStream = connection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            walletChannel.sBitmap(bitmap);
                            Threadutils_boom.getInstance().runOnUiThread(() -> {
                                if (accountTypeIv !=null) accountTypeIv.setImageBitmap(bitmap);
                            });
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            }
        });

        //设置List高度
        ViewGroup.LayoutParams layoutParams = accountTypeListLv.getLayoutParams();
        if (itemHeight ==0) itemHeight =layoutParams.height;
        layoutParams.height = walletAccountTypeListAdapter.getCount() * itemHeight;
        accountTypeListLv.setLayoutParams(layoutParams);

        setCanceledOnTouchOutside(false);

        findViewById(R.id.dialog_add_account_2_btn_submit).setOnClickListener(v -> {

            UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_SAVE_CLICK"));

            String account1 = Stringutil_boom.getSafeString(account1Et.getText().toString());
            String account2 = Stringutil_boom.getSafeString(account2Et.getText().toString());
            HttpManager_boom.getInstance().bindBankAccount(account1,account2, walletChannelStr,product.gtProduct(),product.gtToken());
            dismiss();
        });

    }

    @Override
    public void show() {
        super.show();
        UserInfoHelper_boom.gInstance().addEventPoint(pageTag+"_START");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        UserInfoHelper_boom.gInstance().addEventPoint(pageTag+"_END");
    }

}
